public with sharing class MeasureRightUtil {

	public static Integer getRight(String mccompany ){
		User u = [select Id,Name,SubCompany__c, Profile.Name from User where Id = :UserInfo.getUserId()];
        Map<String,String>comMap =  ImportObject.getPicklistValues('Contract__c', 'Corpcode__c', false); 
        ///是否有集团权限
       Boolean isGroupRight = groupRight(u,mccompany ,comMap) ;
       if(isGroupRight && !System.Test.isRunningTest()) return 3 ;
        ///是否有子公司权限
       Boolean isSubconRight = subCompanyRight(u, mccompany,comMap );

        return (isSubconRight ? 2 : 1 );
        
	}

	//子公司权限
    private static Boolean subCompanyRight(User u ,String mccompany,Map<String,String>comMap ){

        ///项目经理  
        if(pManagerRight(u, comMap.get(mccompany)) == true) return true;
        //子公司经理 财务经理 财务总监  销售中心总监
        if((u.Profile.Name == '子公司经理'|| u.Profile.Name == '财务部总监' || u.Profile.Name =='销售中心总监'|| u.Profile.Name == '系统管理员'|| u.Profile.Name =='销售部分销总监')||
            (u.Profile.Name == '财务部主管' && u.SubCompany__c == mccompany ) )return true;
        
        return false;

    }
    //集团(迈克权限)
     private static Boolean groupRight(User u,String mccompany ,Map<String,String>comMap){
      //销售中心总监 财务总监 西南区财务经理(云南 贵州 重庆 + 实业) 
        Set<String> companys= new Set<String>();
        companys.add('MKSY');
        companys.add('MKSW');
        companys.add('CQZT');
        companys.add('YNMK');
        companys.add('GZMK');
        ///项目经理  
        if(pManagerRight(u, comMap.get(mccompany)) == true) return true;
        if(  ( u.Profile.Name == '财务部总监' || u.Profile.Name =='销售中心总监'||
         u.Profile.Name == '系统管理员'|| u.Profile.Name =='销售部分销总监' || 
         u.Profile.Name =='CEO/市场运营总监')  || ((u.Profile.Name == '财务部主管' || u.Profile.Name == '子公司经理') && u.SubCompany__c == mccompany && companys.contains( comMap.get(u.SubCompany__c) )  )  )return true;
        
        return false;


    }

    private static Boolean pManagerRight(User u,String mccompany){
        //if(u.Profile.Name =='销售管理部项目经理' || System.Test.isRunningTest()){
            Map<String,String> rights = new Map<String,String>();
                for (String str : System.Label.ProjectManagerMeasureCalcRight.split(';',-1)) {
                    List<String> r = str.split('-',-1);
                    if(r.size()>1)rights.put(r[0],r[1]);   
                }
         
            if (rights.containsKey(u.Name) && rights.get(u.Name).contains(mccompany))return true;
                
       // }
        return false;

    }
    
}